<?php 
namespace xfyzf\submit\controller;
use xfyzf\api\controller\AlipayService_dmf;
use xfyzf\api\controller\AlipayService_notify;
use lib\PayTemplate;
use lib\XFDB;
class Submit{
    
    public function qymck(){//支付宝免ck模板
        $trade_no=input("get")['trade_no'];
        
        $srow =  $this->trade_no_srow($trade_no);
        if(!$srow)sysmsg('该订单号不存在，请返回来源地重新发起请求！');
        $userrow = XFDB::userrow($srow['pid'],'pid');
        $QR_row = XFDB::qrlistrow($srow['qr_id']);
        $outtime         = ($srow['outtime']-time());
        $price			 = ($srow['price']>=0.01)?$srow['price']:'请稍等...';
        $cookie          = $QR_row['cookie']??"";
        $userId          = $QR_row['alipayUId']??'';
        $Xf_MOSHI        = $userrow['INPUTFREE']??'';
        $alipayh5url_4   = ALIH5URL($Xf_MOSHI,$price,$userId,$trade_no);
        $mp3             = xfmp3($srow['price']);
        $type            = $srow['type'];
        if(strpos($_SERVER['HTTP_USER_AGENT'], 'QQ/')||strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger')!==false){
            $startApp = '<a id="startApp2"></a>';
        }else{
            $startApp = '<a id="startApp1"></a>';
        }
        $t = true;
        include submittem.'qymck.php';
    }
    public function dmf(){
        global $httphost,$xf_url;
        $trade_no=input("get")['trade_no'];
        $srow =  $this->trade_no_srow($trade_no);
        if(!$srow)sysmsg('该订单号不存在，请返回来源地重新发起请求！');
        $userrow = XFDB::userrow($srow['pid'],'pid');
        $QR_row = XFDB::qrlistrow($srow['qr_id']);
        $outtime         = ($srow['outtime']-time());
        $price			 = ($srow['price']>=0.01)?$srow['price']:sysmsg('金额不合法！');
        $type = $srow['type'];
        $f2fid          = $QR_row['alipayAppId'];
        $f2fpubli        = $QR_row['appPrivateKey'];//私钥
        $sign           = MD5($trade_no);
        $notifyUrl		= $httphost.'/Submit/F2f_notify';
        
        $signType = 'RSA2';
        $aliPay = new AlipayService_dmf();
        $aliPay->setAppid($f2fid);
        $aliPay->setReturnUrl($httphost);
        $aliPay->setNotifyUrl($notifyUrl);
        $aliPay->setRsaPrivateKey($f2fpubli);
        $aliPay->setTotalFee($price);
        $aliPay->setOutTradeNo($trade_no);
        $aliPay->setOrderName($srow['name']);
        $result = $aliPay->doPay();
        $result = $result['alipay_trade_precreate_response'];
        if($result['code'] && $result['code']=='10000'){
            $alipayh5url_1 = "alipays://platformapi/startapp?saId=10000007&qrcode=".urlencode($result['qr_code']);
            $qrcode = $xf_url.'/api/qrcode?url='.urlencode($result['qr_code']);
        }
        $mp3             = xfmp3($srow['price']);
        $t = true;
        include submittem.'dmf.php';
    }
    public function F2f_notify(){
        header('Content-type:text/html; Charset=utf-8');
        $trade_no = $_REQUEST['out_trade_no'];
        unset($_REQUEST['s']);

        $srow =  $this->trade_no_srow($trade_no);
        if(!$srow)Tips::error('该订单号不存在，请返回来源地重新发起请求！');
        $QR_row = XFDB::qrlistrow($srow['qr_id']);
        $userrow = XFDB::userrow($srow['pid'],'pid');
        $alipayPublicKey=$QR_row['alipayPublicKey'];//支付宝公钥
        $aliPay = new AlipayService_notify($alipayPublicKey);
        $result = $aliPay->rsaCheck($_REQUEST,$_REQUEST['sign_type']);
        if($result===true && $_REQUEST['trade_status'] == 'TRADE_SUCCESS' && $srow['status']==0){
             $xf_pid=$srow['pid'];
             $xf_money=$srow['price'];
             $url=creat_callback($srow);
             $data=get_curl($url['notify']);
             xfhtemail($xf_pid,$xf_money,'支付宝当面付',$srow['name'],$srow['trade_no']);  
             echo 'success';
        }else{
            echo 'fail';
        }
    }
    
    public function Mcode_Pay(){
        global $xf_url;
        $trade_no=input("get")['trade_no'];
        $srow =  $this->trade_no_srow($trade_no);
        if(!$srow)sysmsg('该订单号不存在，请返回来源地重新发起请求！');
        $userrow = XFDB::userrow($srow['pid'],'pid');
        $QR_row = XFDB::qrlistrow($srow['qr_id']);
        $outtime         = ($srow['outtime']-time());
        $price			 = ($srow['price']>=0.01)?$srow['price']:sysmsg('金额不合法！');
        $type            = $srow['type'];
        $name = array(
            'wxpay'=>'微信',
            'alipay'=>'支付宝',
            'qqpay'=>'qq'
            );
        $typeName = $name[$type];
        if($userrow['wxpb_llq']=='1'){
            if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ){
                include submittem.'/jump.php';
                die;
            }
        }
        if($type == 'alipay'){
            $userId = getSubstr(base64_decode($QR_row['cookie']), "CLUB_ALIPAY_COM=", ";");
            $Xf_MOSHI=$userrow['INPUTFREE'];
            
            $alipayh5url_4 = ALIH5URL($Xf_MOSHI,$price,$userId,$trade_no);
        }
        $mp3 = xfmp3($srow['price']);
        $mod = 'XUFEI_Pay';
        $loadfile = PayTemplate::load($mod,$userrow);
        $t = true;
        include $loadfile;
    }
    
    public function Usdt(){
        global $conf;
        $trade_no=input("get")['trade_no'];
        $srow =  $this->trade_no_srow($trade_no);
        if(!$srow)sysmsg('该订单号不存在，请返回来源地重新发起请求！');
        $QR_row = XFDB::qrlistrow($srow['qr_id']);
        $userrow = XFDB::userrow($srow['pid'],'pid');
        $outtime         = ($srow['outtime']-time());
        $price			 = ($srow['price']>=0.01)?$srow['price']:sysmsg('金额不合法！');
        $type			 = daddslashes($srow['type']);
        $typeName        = 'USDT';
        include submittem.'usdt.php';
    }
    
    public function Mcode_get(){
        $trade_no=input("get")['trade_no'];
        $srow =  $this->trade_no_srow($trade_no);
        if(!$srow)sysmsg('该订单号不存在，请返回来源地重新发起请求！');
       $QR_row = XFDB::qrlistrow($srow['qr_id']);
        $userrow = XFDB::userrow($srow['pid'],'pid');
        if($srow['status']==1){
        	$url  = creat_callback($srow);//订单支付成功
        	$data = array("backurl"=>$url['return']);
        	deleteimg($trade_no);
        	exitjson(200,'订单支付成功',$data);
        }
        if($srow['outtime']<=time()){
            deleteimg($trade_no);
            $data=array("backurl"=>($_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://').getdomain($srow['return_url']));
            exitjson(-1,'订单支付超时',$data);
        }
        $data =  datatypeqrcode($srow,$QR_row,$userrow,$trade_no);
        exitjson(100,'请扫码支付',$data);
    }
    
    private function trade_no_srow($trade_no){
        $sql = "SELECT * FROM pay_order WHERE trade_no=:trade_no limit 1";
        $params = [
                'trade_no' => $trade_no
            ];
        $srow =  safeSqlQuery($sql, $params)['result'][0];
        return $srow;
    }
    
}


?>